<?php

function gadget_header() {
	$ModulePrefs = "";
	$global_set = parse_ini_file("../global.ini");

	foreach($global_set as $k => $v) {
		$ModulePrefs .= " $k=\"$v\" ";
	}

	$fileInfo = pathinfo($_SERVER['PHP_SELF']);
	$fileName = $fileInfo['filename'];
	$local_set =  parse_ini_file("$fileName.ini");

	$title = "ClickTalk";

	foreach($local_set as $k => $v) {
		$ModulePrefs .= " $k=\"$v\" ";
	}

	$imgUrl = "http://clicktalk.googlecode.com/svn/trunk/gadgets/$fileName/";
	$thumbnail = $imgUrl . "thumbnail.png";
	$screenshot = $imgUrl . "screenshot.png";

	$str = "
<?xml version=\"1.0\" encoding=\"UTF-8\" ?>
<Module>
<ModulePrefs $ModulePrefs screenshot=\"$screenshot\" thumbnail=\"$thumbnail\">
<Require feature=\"dynamic-height\"/>
</ModulePrefs>
<Content type=\"html\">
<![CDATA[ 
	";
	$str = mb_convert_encoding($str, "UTF-8", "GBK");
	return $str;
}

function gadget_footer() {
	$str =  "
			<script src='http://clicktalk.googlecode.com/svn/trunk/gadgets/gfooter.js'></script>
			]]></Content></Module>";
	return $str;
}

function gadget_write() {
	//this script's url
	$s = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . "?lite=1";
	if(isset($_GET['w'])) {
		$h = gadget_header();
		$f = gadget_footer();
		$c = file_get_contents($s);
		$str = $h . $c . $f;
		$str = trim($str); //Null frise line make error in firefox2
		$fileInfo = pathinfo($_SERVER['PHP_SELF']);
		$fileName = $fileInfo['filename'];
		file_put_contents("$fileName.xml", $str);
	
	}
}

//Used for local dev
function html_header() {
	if(!isset($_GET['lite'])) {
		echo '<html><head><meta http-equiv="content-type" content="text/html; charset=UTF-8"><title>Gadget sandbox</title></head><body>';
	}
}

function html_footer() {
	if(!isset($_GET['lite'])) {
		echo "<script src='../gfooter.js'></script></body></html>";
	}
}
?>
